package 抽象数据类型;


import java.util.ArrayList;

public class TreeNode<T> {
    public T val;
    public TreeNode<T> left;
    public TreeNode<T> right;

    public TreeNode(T x) {
        val = x;
    }

    public T getVal() {
        return val;
    }

    public void setVal(T val) {
        this.val = val;
    }

    public TreeNode<T> getLeft() {
        return left;
    }

    public void setLeft(TreeNode<T> left) {
        this.left = left;
    }

    public TreeNode<T> getRight() {
        return right;
    }

    public void setRight(TreeNode<T> right) {
        this.right = right;
    }
    public static TreeNode buildTree(ArrayList<Integer> list) {
        if (list.size() == 0) {
            return null;
        }
        ArrayList<TreeNode> nodeList = new ArrayList();
        for (Object s : list) {
            if (s == null) {
                nodeList.add(null);
            } else {
                Integer val = Integer.parseInt(s.toString());
                nodeList.add(new TreeNode(val));

            }
        }

        TreeNode root = nodeList.get(0);
        for (int i = 0; i < nodeList.size() && 2 * i + 2 < nodeList.size(); i++) {
            if (nodeList.get(i) != null) {
                if (nodeList.get(2 * i + 1) != null) {
                    nodeList.get(i).setLeft(nodeList.get((2 * i + 1)));
                }

                if (nodeList.get(2 * i + 2) != null) {

                    nodeList.get(i).setRight(nodeList.get((2 * i + 2)));

                }
            }


        }
        return root;
    }

}
